/*
 * Created by SharpDevelop.
 * User: Imgen
 * Date: 4/10/2009
 * Time: 11:49 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Text;
using System.Collections.Generic;

using iTextSharp.text;
using iTextSharp.text.pdf;



namespace iPdf
{
    internal abstract class Hyperlink
    {
        protected List<Rectangle> rectangles;
        protected int linkPageNo;

        public int PageNo
        {
            get
            {
                return linkPageNo;
            }
            set
            {
                linkPageNo = value;
            }
        }

        public Hyperlink(int linkPageNo)
        {
            this.linkPageNo = linkPageNo;
            rectangles = new List<Rectangle>();
        }
        public void AddRectangle(Rectangle rect)
        {
            rectangles.Add(rect);
        }

        public void AddRectangles(Rectangle[] rects)
        {
            rectangles.AddRange(rects);
        }

        abstract public void AddAnnotation(PdfStamper stamper);

        protected void AddAnnotation(PdfStamper stamper, PdfAction action)
        {
            foreach (Rectangle rect in rectangles)
            {
                PdfAnnotation annotation = 
#if DEBUG
                    PdfAnnotation.CreateLink(stamper.Writer, rect,PdfAnnotation.HIGHLIGHT_INVERT, action);
#else
                    new PdfAnnotation(stamper.Writer, rect.Left, rect.Bottom, rect.Right, rect.Top, action);
#endif
                stamper.AddAnnotation(annotation, linkPageNo);
            }
        }

        public static bool IsMailToLink(string address)
        {
            string mailToHeader = "mailto:";
            if (address.Length > mailToHeader.Length &&
                address.Substring(0, mailToHeader.Length) == mailToHeader)
            {
                return true;
            }

            return false;
        }

        //judge if the address is a web link like http://xxx or ftp://xxx or mms://
        public static bool IsWebLink(string address)
        {
            string weblinkPattern = "://";
            if (address.Length > 0 && address.IndexOf(weblinkPattern) != -1)
            {
                return true;
            }

            return false;
        }
    }

    internal class LocalDestination : Hyperlink
    {
        private int targetPageNo;
        private float tatgetY;
        
        public LocalDestination(int linkPageNo, int targetPageNo, float tatgetY)
            : base(linkPageNo)
        {
            this.targetPageNo = targetPageNo;
            this.tatgetY = tatgetY;
        }

        public override void AddAnnotation(PdfStamper stamper)
        {
            PdfDestination dest = new PdfDestination(PdfDestination.FITH, tatgetY);
            PdfAction action = PdfAction.GotoLocalPage(targetPageNo, dest, stamper.Writer);

            AddAnnotation(stamper, action);
        }
    }

    internal class RemoteLink : Hyperlink
    {
        private string address;

        public RemoteLink(int linkPageNo, string address)
            : base(linkPageNo)
        {
            this.address = address;
        }

        public override void AddAnnotation(PdfStamper stamper)
        {
            AddAnnotation(stamper, new PdfAction(address));
        }
    }

    internal class MailTo : RemoteLink
    {
        private string mailSubject;
        public MailTo(int linkPageNo, string address, string subject):
            base(linkPageNo, address)
        {
            mailSubject = subject;
        }
    }
}
